home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP06.ZIP / CHAP06 / COSCHMOO / COSCHMOO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  14KB  |  610 lines

  1. /*
  2.  * COSCHMOO.CPP
  3.  * Component Schmoo Chapter 6
  4.  *
  5.  * WinMain and CSchmooFrame implementations.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18. #define INITGUIDS
  19. #include "coschmoo.h"
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     LPCSchmooFrame  pFR;
  34.     FRAMEINIT       fi;
  35.     WPARAM          wRet;
  36.  
  37.    #ifdef WIN32
  38.     SetMessageQueue(96);
  39.    #endif
  40.  
  41.     //Attempt to allocate and initialize the application
  42.     pFR=new CSchmooFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  43.  
  44.     fi.idsMin=IDS_FRAMEMIN;
  45.     fi.idsMax=IDS_FRAMEMAX;
  46.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  47.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  48.     fi.idStatMenuMin=ID_MENUFILE;
  49.     fi.idStatMenuMax=ID_MENUHELP;
  50.     fi.iPosWindowMenu=WINDOW_MENU;
  51.     fi.cMenus=CMENUS;
  52.  
  53.     //If we can initialize pFR, start chugging messages
  54.     if (pFR->FInit(&fi))
  55.         wRet=pFR->MessageLoop();
  56.  
  57.     delete pFR;
  58.     return wRet;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CSchmooFrame::CSchmooFrame
  66.  * CSchmooFrame::~CSchmooFrame
  67.  *
  68.  * Constructor Parameters:
  69.  *  hInst           HINSTANCE from WinMain
  70.  *  hInstPrev       HINSTANCE from WinMain
  71.  *  pszCmdLine      LPSTR from WinMain
  72.  *  nCmdShow        int from WInMain
  73.  */
  74.  
  75. CSchmooFrame::CSchmooFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  76.     , LPSTR pszCmdLine, int nCmdShow)
  77.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  78.     {
  79.     UINT        i;
  80.  
  81.     for (i=0; i<5; i++)
  82.         m_hBmpLines[i]=NULL;
  83.  
  84.     m_fInitialized=FALSE;
  85.     return;
  86.     }
  87.  
  88.  
  89. CSchmooFrame::~CSchmooFrame(void)
  90.     {
  91.     UINT        i;
  92.  
  93.     for (i=0; i<5; i++)
  94.         {
  95.         if (NULL!=m_hBmpLines[i])
  96.             DeleteObject(m_hBmpLines[i]);
  97.         }
  98.  
  99.     if (m_fInitialized)
  100.         CoUninitialize();
  101.  
  102.     return;
  103.     }
  104.  
  105.  
  106.  
  107.  
  108. /*
  109.  * CSchmooFrame::FInit
  110.  *
  111.  * Purpose:
  112.  *  Call CoInitialize then calling down into the base class
  113.  *  initialization.
  114.  *
  115.  * Parameters:
  116.  *  pFI             LPFRAMEINIT containing initialization parameters.
  117.  *
  118.  * Return Value:
  119.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  120.  */
  121.  
  122. BOOL CSchmooFrame::FInit(LPFRAMEINIT pFI)
  123.     {
  124.     DWORD       dwVer;
  125.  
  126.     dwVer=CoBuildVersion();
  127.  
  128.     if (rmm!=HIWORD(dwVer))
  129.         return FALSE;
  130.  
  131.     if (FAILED(CoInitialize(NULL)))
  132.         return FALSE;
  133.  
  134.     m_fInitialized=TRUE;
  135.  
  136.     return CFrame::FInit(pFI);
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. /*
  144.  * CSchmooFrame::CreateCClient
  145.  *
  146.  * Purpose:
  147.  *  Constructs a new client specific to the application.
  148.  *
  149.  * Parameters:
  150.  *  None
  151.  *
  152.  * Return Value:
  153.  *  LPCClient       Pointer to the new client object.
  154.  */
  155.  
  156. LPCClient CSchmooFrame::CreateCClient(void)
  157.     {
  158.     return (LPCClient)(new CSchmooClient(m_hInst));
  159.     }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.  * CSchmooFrame::FPreShowInit
  168.  *
  169.  * Purpose:
  170.  *  Called from FInit before intially showing the window.  We do whatever
  171.  *  else we want here, modifying nCmdShow as necessary which affects
  172.  *  ShowWindow in FInit.
  173.  *
  174.  * Parameters:
  175.  *  None
  176.  *
  177.  * Return Value:
  178.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  179.  */
  180.  
  181. BOOL CSchmooFrame::FPreShowInit(void)
  182.     {
  183.     CreateLineMenu();
  184.     CheckLineSelection(IDM_LINESOLID);
  185.     m_pGB->Check(IDM_LINESOLID, TRUE);
  186.     return TRUE;
  187.     }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. /*
  195.  * CSchmooFrame::CreateLineMenu
  196.  *
  197.  * Purpose:
  198.  *  Initializes the bitmaps used to create the Line menu and replaces
  199.  *  the text items defined in the application resources with these
  200.  *  bitmaps.  Note that the contents of m_hBmpLines must be cleaned
  201.  *  up when the application terminates.
  202.  *
  203.  * Parameters:
  204.  *  None
  205.  *
  206.  * Return Value:
  207.  *  None
  208.  */
  209.  
  210. void CSchmooFrame::CreateLineMenu(void)
  211.     {
  212.     HMENU       hMenu;
  213.     HDC         hDC, hMemDC;
  214.     HPEN        hPen;
  215.     HGDIOBJ     hObj;
  216.     TEXTMETRIC  tm;
  217.     UINT        i, cx, cy;
  218.  
  219.  
  220.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  221.     hDC=GetDC(m_hWnd);
  222.  
  223.     //Create each line in a menu item 8 chars wide, one char high.
  224.     GetTextMetrics(hDC, &tm);
  225.     cx=tm.tmAveCharWidth*8;
  226.     cy=tm.tmHeight;
  227.  
  228.     //Create a memory DC in which to draw lines, and bitmaps for each line.
  229.     hMemDC=CreateCompatibleDC(hDC);
  230.     ReleaseDC(m_hWnd, hDC);
  231.  
  232.     for (i=0; i<5; i++)
  233.         {
  234.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  235.         SelectObject(hMemDC, m_hBmpLines[i]);
  236.  
  237.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  238.  
  239.         hPen=CreatePen(i, 1, 0L);           //i==line style like PS_SOLID
  240.         hObj=SelectObject(hMemDC, hPen);
  241.  
  242.         MoveTo(hMemDC, 0,  cy/2);
  243.         LineTo(hMemDC, cx, cy/2);
  244.  
  245.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  246.             , IDM_LINEMIN+i, (LPCSTR)MAKELONG(m_hBmpLines[i], 0));
  247.  
  248.         SelectObject(hMemDC, hObj);
  249.         DeleteObject(hPen);
  250.         }
  251.  
  252.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  253.     DeleteDC(hMemDC);
  254.  
  255.     return;
  256.     }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. /*
  267.  * CSchmooFrame::CreateGizmos
  268.  *
  269.  * Purpose:
  270.  *  Procedure to create all the necessary gizmobar buttons.
  271.  *
  272.  * Parameters:
  273.  *  None
  274.  *
  275.  * Return Value:
  276.  *  UINT            Number of gizmos added to the bar.
  277.  */
  278.  
  279. UINT CSchmooFrame::CreateGizmos(void)
  280.     {
  281.     UINT            iLast;
  282.     UINT            uState=GIZMO_NORMAL;
  283.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  284.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  285.  
  286.     //Insert the standard ones.
  287.     iLast=CFrame::CreateGizmos();
  288.  
  289.     //Insert File Import in the 5th position and account for it in iLast.
  290.     m_pGB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  291.     iLast++;
  292.  
  293.     //Separator
  294.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  295.  
  296.     //For the Background bitmap, preserve our use of black (part of the image)
  297.     m_pGB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB, NULL, m_hBmp, 3
  298.                , GIZMO_NORMAL | PRESERVE_BLACK);
  299.  
  300.     m_pGB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  301.  
  302.     //Separator
  303.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  304.  
  305.     //Line styles.
  306.     m_pGB->Add(utEx, iLast++, IDM_LINESOLID,      m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  307.     m_pGB->Add(utEx, iLast++, IDM_LINEDASH,       m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  308.     m_pGB->Add(utEx, iLast++, IDM_LINEDOT,        m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  309.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOT,    m_dxB, m_dyB, NULL, m_hBmp, 8, uState);
  310.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB, NULL, m_hBmp, 9, uState);
  311.  
  312.     return iLast;
  313.     }
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. /*
  323.  * CSchmooFrame::OnCommand
  324.  *
  325.  * Purpose:
  326.  *  WM_COMMAND handler for the Schmoo frame window that just processes
  327.  *  the line menu and the color menu leaving the CFrame to do everything
  328.  *  else.
  329.  *
  330.  * Parameters:
  331.  *  hWnd            HWND of the frame window.
  332.  *  wParam          WPARAM of the message.
  333.  *  lParam          LPARAM of the message.
  334.  *
  335.  * Return Value:
  336.  *  LRESULT         Return value for the message.
  337.  */
  338.  
  339. LRESULT CSchmooFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  340.     {
  341.     LPCSchmooDoc    pDoc;
  342.     char            szFile[CCHPATHMAX];
  343.     BOOL            fOK;
  344.     UINT            i, uTemp;
  345.     COLORREF        rgColors[16];
  346.     CHOOSECOLOR     cc;
  347.  
  348.     COMMANDPARAMS(wID, wCode, hWndMsg);
  349.  
  350.     /*
  351.      * Don't bother with anything during first initialization,
  352.      * skipping many GizmoBar notifications.
  353.      */
  354.     if (m_fInit)
  355.         return 0L;
  356.  
  357.     pDoc=(LPCSchmooDoc)m_pCL->ActiveDocument();
  358.  
  359.     /*
  360.      * Check for the line style commands which are IDM_LINEMIN+<style>.
  361.      * We handle this by changing the menu and toolbar, then we pass
  362.      * it to the document for real processing.
  363.      */
  364.     if (NULL!=pDoc && IDM_LINEMIN <= wID && IDM_LINEMAX >=wID)
  365.         {
  366.         CheckLineSelection(wID);
  367.         pDoc->LineStyleSet(wID-IDM_LINEMIN);
  368.         return 0L;
  369.         }
  370.  
  371.     switch (wID)
  372.         {
  373.         case IDM_FILEIMPORT:
  374.             szFile[0]=0;
  375.             fOK=FSaveOpenDialog(szFile, CCHPATHMAX, IDS_FILEIMPORT, TRUE, &i);
  376.  
  377.             if (fOK)
  378.                 {
  379.                 uTemp=pDoc->ULoad(FALSE, szFile);
  380.                 pDoc->ErrorMessage(uTemp);
  381.                 }
  382.  
  383.             return (LRESULT)fOK;
  384.  
  385.  
  386.         case IDM_COLORBACKGROUND:
  387.         case IDM_COLORLINE:
  388.             //Invoke the color chooser for either color
  389.             uTemp=(IDM_COLORBACKGROUND==wID)
  390.                 ? DOCCOLOR_BACKGROUND : DOCCOLOR_LINE;
  391.  
  392.             for (i=0; i<16; i++)
  393.                 rgColors[i]=RGB(0, 0, i*16);
  394.  
  395.             memset(&cc, 0, sizeof(CHOOSECOLOR));
  396.             cc.lStructSize=sizeof(CHOOSECOLOR);
  397.             cc.lpCustColors=rgColors;
  398.             cc.hwndOwner=hWnd;
  399.             cc.Flags=CC_RGBINIT;
  400.             cc.rgbResult=pDoc->ColorGet(uTemp);
  401.  
  402.             if (ChooseColor(&cc))
  403.                 pDoc->ColorSet(uTemp, cc.rgbResult);
  404.  
  405.             break;
  406.  
  407.  
  408.         default:
  409.            CFrame::OnCommand(hWnd, wParam, lParam);
  410.         }
  411.  
  412.     return 0L;
  413.     }
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420. /*
  421.  * CSchmooFrame::OnDocumentDataChange
  422.  *
  423.  * Purpose:
  424.  *  Update the Line menu and GizmoBar if the style in the data changes.
  425.  *
  426.  * Parameters:
  427.  *  pDoc            LPCDocument notifying the sink.
  428.  *
  429.  * Return Value:
  430.  *  None
  431.  */
  432.  
  433. void CSchmooFrame::OnDocumentDataChange(LPCDocument pDoc)
  434.     {
  435.     CheckLineSelection(IDM_LINEMIN+((LPCSchmooDoc)pDoc)->LineStyleGet());
  436.     return;
  437.     }
  438.  
  439.  
  440.  
  441.  
  442. /*
  443.  * CSchmooFrame::OnDocumentActivate
  444.  *
  445.  * Purpose:
  446.  *  Informs us that document activation changed, so update the UI for
  447.  *  that new document.
  448.  *
  449.  * Parameters:
  450.  *  pDoc            LPCDocument notifying the sink.
  451.  *
  452.  * Return Value:
  453.  *  None
  454.  */
  455.  
  456. void CSchmooFrame::OnDocumentActivate(LPCDocument pDoc)
  457.     {
  458.     CheckLineSelection(IDM_LINEMIN+((LPCSchmooDoc)pDoc)->LineStyleGet());
  459.     return;
  460.     }
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468. /*
  469.  * CSchmooFrame::UpdateMenus
  470.  *
  471.  * Purpose:
  472.  *  Handles the WM_INITMENU message for the frame window.  Depending
  473.  *  on the existence of an active window, menu items are selectively
  474.  *  enabled and disabled.
  475.  *
  476.  * Parameters:
  477.  *  hMenu           HMENU of the menu to intialize
  478.  *  iMenu           UINT position of the menu.
  479.  *
  480.  * Return Value:
  481.  *  None
  482.  */
  483.  
  484. void CSchmooFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  485.     {
  486.     LPCDocument pDoc;
  487.     BOOL        fOK=FALSE;
  488.     BOOL        fCallDefault=TRUE;
  489.     UINT        i;
  490.     UINT        uTemp;
  491.     UINT        uTempE;
  492.     UINT        uTempD;
  493.  
  494.     pDoc=m_pCL->ActiveDocument();
  495.  
  496.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  497.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  498.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  499.  
  500.     //File menu:  If there is no current document window, disable Import.
  501.     if (m_phMenu[0]==hMenu)
  502.         EnableMenuItem(hMenu, IDM_FILEIMPORT, uTemp);
  503.  
  504.     //Color menu:  no document, no commands
  505.     if (m_phMenu[2]==hMenu)
  506.         {
  507.         EnableMenuItem(hMenu, IDM_COLORBACKGROUND, uTemp);
  508.         EnableMenuItem(hMenu, IDM_COLORLINE,       uTemp);
  509.         fCallDefault=FALSE;
  510.         }
  511.  
  512.     //Line menu:  no document, no commands
  513.     if (m_phMenu[3]==hMenu)
  514.         {
  515.         for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  516.             EnableMenuItem(hMenu, i, uTemp);
  517.  
  518.         fCallDefault=FALSE;
  519.         }
  520.  
  521.     if (fCallDefault)
  522.         CFrame::UpdateMenus(hMenu, iMenu);
  523.  
  524.     return;
  525.     }
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. /*
  533.  * CSchmooFrame::UpdateGizmos
  534.  *
  535.  * Purpose:
  536.  *  Enables and disables gizmos depending on whether we have
  537.  *  a document or not.
  538.  *
  539.  * Parameters:
  540.  *  None
  541.  *
  542.  * Return Value:
  543.  *  None
  544.  */
  545.  
  546. void CSchmooFrame::UpdateGizmos(void)
  547.     {
  548.     LPCDocument pDoc;
  549.     BOOL        fEnable;
  550.     UINT        i;
  551.  
  552.     //Let the default hack on its gizmos.
  553.     CFrame::UpdateGizmos();
  554.  
  555.     pDoc=m_pCL->ActiveDocument();
  556.     fEnable=(NULL!=pDoc);
  557.  
  558.     //No document, disable just about everything
  559.     m_pGB->Enable(IDM_FILEIMPORT, fEnable);
  560.  
  561.     m_pGB->Enable(IDM_COLORBACKGROUND, fEnable);
  562.     m_pGB->Enable(IDM_COLORLINE,       fEnable);
  563.  
  564.     for (i=IDM_LINEMIN; i <= IDM_LINEMAX; i++)
  565.         m_pGB->Enable(i, fEnable);
  566.  
  567.     return;
  568.     }
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575. /*
  576.  * CSchmooFrame::CheckLineSelection
  577.  *
  578.  * Purpose:
  579.  *  Maintains the bitmap menu and the gizmos for the line selection.  Both
  580.  *  are mutially exclusive option lists where a selection in one has to
  581.  *  affect the other.
  582.  *
  583.  * Parameters:
  584.  *  uID             UINT ID of the item to be selected
  585.  *
  586.  * Return Value:
  587.  *  None
  588.  */
  589.  
  590. void CSchmooFrame::CheckLineSelection(UINT uID)
  591.     {
  592.     UINT        i;
  593.     HMENU       hMenu;
  594.     LPCDocument pDoc;
  595.  
  596.     hMenu=GetMenu(m_hWnd);
  597.  
  598.     //Uncheck all lines initially.
  599.     for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  600.         CheckMenuItem(hMenu, i, MF_UNCHECKED | MF_BYCOMMAND);
  601.  
  602.     CheckMenuItem(hMenu, uID, MF_CHECKED | MF_BYCOMMAND);
  603.     m_pGB->Check(uID, TRUE);
  604.  
  605.     pDoc=m_pCL->ActiveDocument();
  606.     m_pGB->Enable(uID, (NULL!=pDoc));
  607.  
  608.     return;
  609.     }
  610.